package edu.gatech.cs2340.spring2012.team38.database;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class UserDBHelper extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "users.db";
	private static final int DATABASE_VERSION = 1;
	private SQLiteDatabase db1;

	/**
	 * Constructor. Makes a call to the super class with a context instance.
	 * @param context The context of the app. Allows the system to use the 
	 * database without much fuss. Used internally in package.
	 */
	public UserDBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	/**
	 * Create the database. Static call to UserTable class.
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		UserTable.onCreate(db);
		db1 = db;
	}
	
	/**
	 * Checks to see if user exists and matches username to password
	 * @param username The passed in username
	 * @param password The passed in password
	 * @return Returns true if the validation is successful and false if not
	 */
	public boolean validateUser(String username, String password){
		Cursor c = getReadableDatabase().rawQuery(
				"SELECT * FROM " + UserTable.TABLE_USERS + " WHERE "
		        + UserTable.COLUMN_USERNAME + "='" + username +"'AND "+
		        UserTable.COLUMN_PASSWORD+"='"+password+"'" ,  null);
		if (c.getCount()>0)
			return true;
		    return false;
	}

	/**
	 * Upgrade database. Static call to UserTable class.
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		UserTable.onUpgrade(db, oldVersion, newVersion);
	}
	
	public SQLiteDatabase getDB(){
		return db1;
	}

}
